package com.ljq.servicepassengeruser.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljq.intemalcommon.dto.ResponseResult;
import com.ljq.servicepassengeruser.dto.PassengerUser;
import com.ljq.servicepassengeruser.mapper.PassengerUserMapper;
import com.ljq.servicepassengeruser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.time.LocalDateTime;


@Service
public class UserServiceImpl extends ServiceImpl<PassengerUserMapper, PassengerUser> implements UserService  {
    @Autowired
    private PassengerUserMapper passengerUserMapper;

    @Override
    public ResponseResult loginorReg(String phone, String code) {
        //根据手机号查询用户信息
        PassengerUser user =  passengerUserMapper.selectOne(Wrappers.<PassengerUser>lambdaQuery().eq(PassengerUser::getPassengerPhone,phone));
        //判断用户信息是否存在
        if (ObjectUtils.isEmpty(user)){
            user = new PassengerUser();
            //如果不存在，则插入用户信息
            user.setPassengerPhone(phone);
            user.setPassengerName("张三");
            user.setState((byte) 1);
            user.setPassengerGender((byte) 1);
            user.setGmtCreate(LocalDateTime.now());
            user.setGmtModified(user.getGmtCreate());
            passengerUserMapper.insert(user);
            return ResponseResult.success("注册成功");
        }

        //校验token信息
        return ResponseResult.success("登录成功");
    }
}
